/**

 */

//*** Ping the Stat Server *** ///
statFeedNo = 1444;
loadScript("http://myDivXConnected.com/pMaker/includes/statsPush.js");

loadScript("page-parse.js");
loadScript("shows.js");
loadScript("show-parse.js");
loadScript("playback.js");
loadScript("popup.js");
loadScript("info-popup.js");
loadScript("meta-data.js");


var version = "1.5.3.2";
var queueId = "";

var serviceXHR;

var feed;
var feedTitle = "HULU VIDEO FEEDS";

var itemsNum;
var buildDate;

var movieTitle;
var movieDescription;
var movieImage;
var movieUrl;
var embedUrl;

var videoId;

var listItems = new Array();
var movieList;

var cursorPosition = 1;

var huluStorage = "hulu";
var huluQueueId;

var state;

// XML Namespace resolver for XPath namespace resolution
function NSResolver(prefix) {
  var ns = {
    'media' : 'http://search.yahoo.com/mrss/'
  };
  return ns[prefix] || null;
}

// Takes a definition document and populates the various DCTK controls
function handleServiceResult(doc)
{
    // extract information with XPath
	
	itemsNum = doc.evaluate("count(//item)", doc, NSResolver, XPathResult.ANY_TYPE, null).numberValue;
	
	buildDate = doc.evaluate("//channel/pubDate", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	
	movieTitle = new Array()
	for (var i = 1; i <= itemsNum; i++){
		movieTitle[i] = doc.evaluate("//item[" + i + "]/title", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	movieDescription = new Array()
	for (var i = 1; i <= itemsNum; i++){
		movieDescription[i] = doc.evaluate("//item[" + i + "]/pubDate", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	movieImage = new Array()
	for (var i = 1; i <= itemsNum; i++){
		movieImage[i] = doc.evaluate("//item[" + i + "]/media:thumbnail/@url", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
		
	embedUrl = new Array()
	for (var i = 1; i <= itemsNum; i++){
		embedUrl[i] = doc.evaluate("//item[" + i + "]/media:player/@url", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
		
	movieUrl = new Array()
	for (var i = 1; i <= itemsNum; i++){
		movieUrl[i] = doc.evaluate("//item[" + i + "]/guid", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
    
    var dctk = connected.toolkit;
	
	dateTxt = dctk.getElementById("dateTxt");
    dateTxt.text = "Last Updated: "+buildDate;
	
	connected.debug.print(movieTitle[4]);
	connected.debug.print(movieDescription[4]);
	connected.debug.print(movieUrl[4]);

	createItemList();
}

function onServiceXHRStateChange()
{
    if (serviceXHR.readyState == 4) {
        if (serviceXHR.status == 200) {
            handleServiceResult(serviceXHR.responseXML);
        }
    }
}

function onLoadVideos()
{
	
	serviceXHR = new XMLHttpRequest();
	serviceXHR.open("GET", feed, true);
	serviceXHR.overrideMimeType('text/xml');
	serviceXHR.onreadystatechange = onServiceXHRStateChange;
	serviceXHR.send(null); 

}

function mainButtonHandler(e)
{
    switch(e.buttonId)
    {
        case dcKeyEvent.BUTTON_ID_BACK:
        {
            // navigate back
			if (state == "home" && window.history.length)
                window.history.back();
			else if (state == "popShows") 
			{
				feed = "http://www.hulu.com/feed/popular/episode/today?kind=shows";
				feedTitle = "POPULAR SHOWS";
				state = "";
				doShowsLayout();
			}
			else if (state == "myShows") 
			{
				feed = "http://www.hulu.com/feed/subscriptions/"+queueId;
				feedTitle = "MY SUBSCRIPTIONS";
				state = "";
				doShowsLayout();
			}
			else 
				onLoadComplete();
        }
		break;
		

		case dcKeyEvent.BUTTON_ID_MENU:
        {
			showQueueIdPopup();
        }
        break;
		
		case dcKeyEvent.BUTTON_ID_INFO:
		{
			showInfoPopup();
		}
		break;


    }
}

function folderSelected()
{
	cursorPosition = movieList.curSel;
	if (cursorPosition == 0)
	{
		feed = "http://www.hulu.com/feed/popular/episode/today?kind=shows";
		feedTitle = "POPULAR SHOWS";
		state = "";
		doShowsLayout();
	}
	else if (cursorPosition == 1)
	{
		feed = "http://www.hulu.com/feed/popular/tv";
		feedTitle = "POPULAR EPISODES";
		state = "";
		doVideoLayout();
	}
	else if (cursorPosition == 2)
	{
		feed = "http://www.hulu.com/feed/popular/feature_film/today";
		feedTitle = "POPULAR MOVIES";
		state = "";
		doVideoLayout();
	}
	else if (cursorPosition == 3 && queueId != "")
	{
		feed = "http://www.hulu.com/feed/queue/"+queueId;
		feedTitle = "MY HULU QUEUE";
		state = "";
		doVideoLayout();
	}
	else if (cursorPosition == 3 && queueId == "")
	{
		showSearchPopup();
	}
	else if (cursorPosition == 4 && queueId != "")
	{
		feed = "http://www.hulu.com/feed/subscriptions/"+queueId;
		feedTitle = "MY SUBSCRIPTIONS";
		state = "";
		doShowsLayout();
	}
	else if (cursorPosition == 4 && queueId == "")
	{
		showQueueIdPopup();
	}
	else if (cursorPosition == 5)
	{
		feed = "http://www.hulu.com/feed/recommendations/"+queueId;
		feedTitle = "RECOMMENDATIONS";
		state = "";
		doVideoLayout();
	}
	else if (cursorPosition == 6)
	{
		showSearchPopup();
	}
	
	else if (cursorPosition == 7)
	{
		showQueueIdPopup();
	}	
	
}

function onLoadComplete()
{
    var dctk = connected.toolkit;

    dctk.logicalWidth = 640;
    dctk.logicalHeight = 480;
    
    // create root panel
	var root = dctk.createPanelCtrl("root", true);
	root.width = 640;
	root.height = 480;
	root.backgroundStyle = dctkIElement.BACKGROUND_STYLE_IMAGE;
	root.backgroundImg.src = "hulu-back.png"
	root.color.rgb = 0xFFFFFF;
	root.onUnhandledKeyEvent = mainButtonHandler;
	dctk.rootPanel = root;
	
	var header = dctk.createPanelCtrl("header", true);
	header.setRect(0, 413, 640, 480);
	header.filled = true;
	header.color.rgb = 0xf2f2f2;
	root.addChild(header);
	
	var footer = dctk.createPanelCtrl("footer", true);
	footer.width = 640;
	footer.height = 38;
	footer.filled = true;
	footer.color.rgb = 0xf2f2f2;
	root.addChild(footer);
	
	var logo = dctk.createImageCtrl("logo", true);
	logo.setRect(15, 415, 90, 478);
	logo.img.src = "hulu-logo.png";
	root.addChild(logo);
	
	feedTitle = "HULU VIDEO FEEDS";
	state = "home";
	
	var storage = connected.openStorage(huluStorage,true);
	queueId = storage.get(huluQueueId,queueId);
	
	doLayout();

}

function doLayout()
{
	var dctk = connected.toolkit;

    dctk.logicalWidth = 640;
    dctk.logicalHeight = 480;
    
    // create root panel
	var root = dctk.createPanelCtrl("root", true);
	root.width = 640;
	root.height = 480;
	root.backgroundStyle = dctkIElement.BACKGROUND_STYLE_IMAGE;
	root.backgroundImg.src = "hulu-back.png"
	root.color.rgb = 0xFFFFFF;
	root.onUnhandledKeyEvent = mainButtonHandler;
	dctk.rootPanel = root;
	
	var header = dctk.createPanelCtrl("header", true);
	header.setRect(0, 413, 640, 480);
	header.filled = true;
	header.color.rgb = 0xf2f2f2;
	root.addChild(header);
	
	var footer = dctk.createPanelCtrl("footer", true);
	footer.width = 640;
	footer.height = 38;
	footer.filled = true;
	footer.color.rgb = 0xf2f2f2;
	root.addChild(footer);
	
	var logo = dctk.createImageCtrl("logo", true);
	logo.setRect(15, 415, 90, 478);
	logo.img.src = "hulu-logo.png";
	root.addChild(logo);
	
	var queueTxt = dctk.createLabelCtrl("queueTxt", true);
    queueTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    queueTxt.setRect(80, 380, 560, 415);
    queueTxt.textColor.rgb = 0x99cc33;
    queueTxt.fontSize = 25;
    queueTxt.align = dctkLabel.TS_CENTER;
	queueTxt.valign = dctkLabel.TS_MIDDLE;
	queueTxt.text = feedTitle;
    root.addChild(queueTxt);
	
	var dateTxt = dctk.createLabelCtrl("dateTxt", true);
    dateTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    dateTxt.setRect(10, 10, 630, 30);
    dateTxt.textColor.rgb = 0x808080;
    dateTxt.fontSize = 20;
    dateTxt.align = dctkLabel.TS_LEFT;
	dateTxt.valign = dctkLabel.TS_MIDDLE;
	dateTxt.text = "";
    root.addChild(dateTxt);
	
	var footerTxt = dctk.createLabelCtrl("footerTxt", true);
    footerTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    footerTxt.setRect(10, 10, 630, 30);
    footerTxt.textColor.rgb = 0x808080;
    footerTxt.fontSize = 20;
    footerTxt.align = dctkLabel.TS_RIGHT;
	footerTxt.valign = dctkLabel.TS_MIDDLE;
	footerTxt.text = "";
    root.addChild(footerTxt);
	
/*
	movieList = dctk.createThumbCtrl("movieList", true);
    movieList.setRect(25, 38, 615, 385);
	movieList.thumbHCount = 4;
    movieList.thumbVCount = 3;
	movieList.fontSize = 14;
	movieList.thumbHGap = 0.08;
	movieList.thumbVGap = 0.08;
	movieList.marquee = true;
    movieList.keyWrap = true;
	movieList.showPageIndicator = true;
	movieList.textColor.rgb = 0xFFFFFF;
	movieList.textSelColor.rgb = 0x000000;
	movieList.labelColor.rgb = 0x000000;
	movieList.labelSelColor.rgb = 0xCCCCCC;
*/
	movieList = dctk.createFilmstripCtrl("movieList", true);
    movieList.setRect(25, 38, 615, 385);
	movieList.reflect = true;
	movieList.style = dctkIFilmstrip.FILMSTRIP_STYLE_HORIZONTAL_MIDDLE;
	movieList.scrollBarWidth = 0;
	movieList.onSelect = folderSelected;
	root.addChild(movieList);
	
	dctk.screenFade(1, 0, 0.5);
	
	homeScreen();
}

function homeScreen()
{
	var item;
	var icon = "chrome://connected/content/icons/folder-lg.png"
	var dctk = connected.toolkit;

		item = movieList.addItem("id-01");
    	item.text = "Popular Shows"
		item.img.src = icon;
		//item.showText = true;
		
		item = movieList.addItem("id-02");
    	item.text = "Popular Episodes"
		item.img.src = "chrome://connected/content/icons/comp.png";
		//item.showText = true;
		
		item = movieList.addItem("id-03");
    	item.text = "Popular Movies"
		item.img.src = "chrome://connected/content/icons/film.png";
		//item.showText = true;
		
		if (queueId != ""){
		item = movieList.addItem("id-04");
    	item.text = "My Queue"
		item.img.src = "chrome://connected/content/icons/folder-stack-lg.png";
		//item.showText = true;
		
		item = movieList.addItem("id-05");
    	item.text = "My Subscriptions";
		item.img.src = "chrome://connected/content/icons/folder-playlist-lg.png";
		//item.showText = true;
		
		item = movieList.addItem("id-06");
    	item.text = "Recommendations"
		item.img.src = "chrome://connected/content/icons/film.png";
		//item.showText = true;
		}
		
		item = movieList.addItem("id-07");
    	item.text = "Simple Video Search"
		item.img.src = "chrome://connected/content/icons/error.png";
		//item.showText = true;
		
		item = movieList.addItem("id-08");
    	item.text = "My Settings"
		item.img.src = "chrome://connected/content/icons/gear.png";
		//item.showText = true;

}

function movieSelected()
{
  cursorPosition = movieList.curSel;
  nowPlaying = movieTitle[cursorPosition+1];
  playDate = movieDescription[cursorPosition+1];
  playDesc = movieImage[cursorPosition+1];
  //showPlaybackPopup();
  
  //eid fix
  pageUrl = movieUrl[cursorPosition+1];
  startPageRequest();
  
  //meta data
	//startMetaRequest();

}

function createItemList(){
	var item;
	var dctk = connected.toolkit;
	for (var i = 1; i <= itemsNum; i++){
		item = movieList.addItem("id-0"+[i]);
    	item.text = i+" - " + movieTitle[i];
		item.img.src = movieImage[i];
		item.showText = true;
	}
}

function doVideoLayout()
{
	var dctk = connected.toolkit;

    dctk.logicalWidth = 640;
    dctk.logicalHeight = 480;
    
    // create root panel
	var root = dctk.createPanelCtrl("root", true);
	root.width = 640;
	root.height = 480;
	root.backgroundStyle = dctkIElement.BACKGROUND_STYLE_IMAGE;
	root.backgroundImg.src = "hulu-back.png"
	root.color.rgb = 0xFFFFFF;
	root.onUnhandledKeyEvent = mainButtonHandler;
	dctk.rootPanel = root;
	
	var header = dctk.createPanelCtrl("header", true);
	header.setRect(0, 413, 640, 480);
	header.filled = true;
	header.color.rgb = 0xf2f2f2;
	root.addChild(header);
	
	var footer = dctk.createPanelCtrl("footer", true);
	footer.width = 640;
	footer.height = 38;
	footer.filled = true;
	footer.color.rgb = 0xf2f2f2;
	root.addChild(footer);
	
	var logo = dctk.createImageCtrl("logo", true);
	logo.setRect(15, 415, 90, 478);
	logo.img.src = "hulu-logo.png";
	root.addChild(logo);
	
	var queueTxt = dctk.createLabelCtrl("queueTxt", true);
    queueTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    queueTxt.setRect(80, 380, 560, 415);
    queueTxt.textColor.rgb = 0x99cc33;
    queueTxt.fontSize = 25;
    queueTxt.align = dctkLabel.TS_CENTER;
	queueTxt.valign = dctkLabel.TS_MIDDLE;
	queueTxt.text = feedTitle;
    root.addChild(queueTxt);
	
	var dateTxt = dctk.createLabelCtrl("dateTxt", true);
    dateTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    dateTxt.setRect(10, 10, 630, 30);
    dateTxt.textColor.rgb = 0x808080;
    dateTxt.fontSize = 20;
    dateTxt.align = dctkLabel.TS_LEFT;
	dateTxt.valign = dctkLabel.TS_MIDDLE;
	dateTxt.text = "";
    root.addChild(dateTxt);
	
	var footerTxt = dctk.createLabelCtrl("footerTxt", true);
    footerTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    footerTxt.setRect(10, 10, 630, 30);
    footerTxt.textColor.rgb = 0x808080;
    footerTxt.fontSize = 20;
    footerTxt.align = dctkLabel.TS_RIGHT;
	footerTxt.valign = dctkLabel.TS_MIDDLE;
	footerTxt.text = "";
    root.addChild(footerTxt);
	
	movieList = dctk.createThumbCtrl("movieList", true);
    movieList.setRect(25, 38, 615, 385);
	movieList.thumbHCount = 4;
    movieList.thumbVCount = 3;
	movieList.fontSize = 14;
	movieList.thumbHGap = 0.08;
	movieList.thumbVGap = 0.08;
	movieList.marquee = true;
    movieList.keyWrap = true;
	movieList.showPageIndicator = true;
	movieList.textColor.rgb = 0xFFFFFF;
	movieList.textSelColor.rgb = 0x000000;
	movieList.labelColor.rgb = 0x000000;
	movieList.labelSelColor.rgb = 0xCCCCCC;
	movieList.onSelect = movieSelected;
	root.addChild(movieList);
	
	dctk.screenFade(1, 0, 0.5);
	
	onLoadVideos();

}
